

layui.use(['table', 'admin', 'ax','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功

    /**
     * 管理
     */
    var TbOrder = {
        tableId: "tbOrderTable"
    };

    /**
     * 初始化表格的列
     */
    TbOrder.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'orderId', sort: true, title: '订单号'},
            {field: 'buyerNick', sort: true, title: '买家昵称'},
            /*{field: 'paymentType', sort: true, title: '支付类型',templet: function (d) {
                    console.log(d.paymentType);
                    var paymentTypeArr = ["在线支付", "货到付款"];
                    return paymentTypeArr[d.paymentType - 1];
                }
            },*/
            {field: 'payment', sort: true, title: '实付金额'},
            {field: 'postFee', sort: true, title: '运费'},
            {field: '' +
                    'money', sort: true, title: '单价'},
            {field: 'tax', sort: true, title: '税金'},
            {field: 'status', sort: true, title: '状态',templet: function (d) {
                    var statusArr = ["待付款", "已付款", "待发货", "已发货", "交易成功", "交易关闭", "交易失败","申请退货","待审查","已退货"];
                    var html
                    if(d.status==8){
                       html='<a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="edit">详情</a>\n' +
                          '\n' +
                          '    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="shenhe">审核</a>\n'

                    }else if(d.status==7){
                        html='<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="tuihuo" >申请退货</a>\n'
                    }else if(d.status==9){
                        html='<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="yituihuo" >已退货</a>\n'
                    }else{
                         html='<a class="layui-btn layui-btn-checked layui-btn-xs" lay-event="edit">详情</a>\n' +
                             '\n' +
                             '    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="shenhe">已审核</a>\n'
                    }
                    $("#tableBar").empty();
                   $("#tableBar").append(html);
                    return statusArr[d.status];
                }
            },
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'paymentTime', sort: true, title: '付款时间'},
            {field: 'consignTime', sort: true, title: '发货时间'},
            {align: 'center', toolbar:'#tableBar', title: '操作'}
        ]];

        form.render();
    };



    /**
     * 上传到管家婆
     */
    TbOrder.shangChuan=function(){
        var checkStatus = table.checkStatus(TbOrder.tableId);
        //console.log(checkStatus.data)//选中行数据
        var items = checkStatus.data.length;
        var myArray=new Array();
        for(var a=0; a<items;a++){
            if(checkStatus.data[a].status !=8){
                myArray[a]=checkStatus.data[a].orderId
            }

        }

        window.location.href = Feng.ctxPath + '/tbOrder/uploadDetail?myArray='+myArray+"&status=10";
     /*  $.ajax({
        url:Feng.ctxPath + '/tbOrder/uploadDetail?myArray='+myArray,
        data:myArray,
        type:"get",
           datatype:"json",
           async:"ture",
        success: function(data){

        }
});*/


    }

    /**
     * 点击查询按钮
     */
    TbOrder.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(TbOrder.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 导出excel按钮
     */
    TbOrder.exportExcel = function () {
        var checkRows = table.checkStatus(TbOrder.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    TbOrder.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/tbOrder/edit?orderId=' + data.orderId+','+data.goodsId;
    };



    /**
     * 点击退货
     *
     * @param data 点击按钮时候的行数据
     */
    TbOrder.ontuihuo = function (data) {
        //debugger;
        //document.getElementById("tui").innerHTML="已退货";
        var ajax = new $ax(Feng.ctxPath + "/tbOrder/updateOrder?userId="+data.userId+"&status="+9, function (data) {
            table.reload(TbOrder.tableId);
        }, function (data) {

        });
        ajax.set("orderId", data.orderId);
        ajax.start();

        layui.table.reload('tbOrderTable', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: ""
        });
        /*location.reload();*/
    };


    /**
     * 发货
     */
    TbOrder.onDeliverItem = function (data) {
        var ajax = new $ax(Feng.ctxPath + "/tbOrder/deliver", function (data) {
            Feng.success("操作成功!");
            table.reload(TbOrder.tableId);
        }, function (data) {
            Feng.error("操作失败!" + data.responseJSON.message + "!");
        });
        ajax.set("orderId", data.orderId);
        ajax.start();
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TbOrder.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tbOrder/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TbOrder.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("orderId", data.orderId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TbOrder.tableId,
        url: Feng.ctxPath + '/tbOrder/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: TbOrder.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TbOrder.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TbOrder.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TbOrder.exportExcel();
    });

    // 上传到管家婆
    $('#shangChuan').click(function () {
        TbOrder.shangChuan();
    });



    // 工具条点击事件
    table.on('tool(' + TbOrder.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TbOrder.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TbOrder.onDeleteItem(data);
        } else if (layEvent === 'deliver') {
            TbOrder.onDeliverItem(data);
        }else if (layEvent === 'tuihuo') {
            TbOrder.ontuihuo(data);
        }
    });
});
